package com.ruoqing.admin.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.api.R;
import com.ruoqing.admin.api.UserHandleService;
import com.ruoqing.core.constant.ModuleConstants;
import com.ruoqing.core.dto.BaseResponse;
import com.ruoqing.core.dto.admin.BanUserPageDto;
import com.ruoqing.core.dto.user.UserPageDto;
import com.ruoqing.core.dto.user.UserResponse;
import com.ruoqing.core.domain.rquser.UserEntity;
import com.ruoqing.core.enums.UserQueryEnum;
import com.ruoqing.core.fallback.downgrade.UserDowngradeMethods;
import com.ruoqing.core.fallback.fuse.UserLimitingMethods;
import com.ruoqing.core.web.BaseRestApi;
import com.ruoqing.log.annotation.BaseLog;
import com.ruoqing.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
	* @author YaoXian
	* @title: UserHandleController
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 后台管理中心 - 用户管理控制层
	* @date 2020-11-15 16:16
	*/
@RestController
@RequestMapping(path = "/users")
@Api(tags = "后台管理中心对外HTTP调用 - 管理员操作用户Rest接口")
public class UserRestApi extends BaseRestApi {
		
		@Resource
		private UserHandleService userHandleService;
		
		@BaseLog(title = "添加用户",module = ModuleConstants.APP_ADMIN,businessType = BusinessType.INSERT)
		@ApiOperation(value = "管理员添加用户", httpMethod = "POST")
		@PostMapping(path = "/add")
		public BaseResponse addUser(String userEntity){
				JSONObject jsonObject = JSONObject.fromObject(userEntity);
				UserEntity user = (UserEntity) JSONObject.toBean(jsonObject, UserEntity.class);
				return userHandleService.insertUser(user);
		}
		
		@BaseLog(title = "修改用户信息",module = ModuleConstants.APP_ADMIN,businessType = BusinessType.UPDATE)
		@ApiOperation(value = "管理员修改用户信息", httpMethod = "POST")
		@ApiImplicitParams(value = {@ApiImplicitParam(name = "userEntity", value = "用户实体类"
						, required = true, dataType = "UserEntity"),@ApiImplicitParam(name = "modifyReason", value = "修改理由"
						, required = true, dataType = "modifyReason")})
		@PostMapping(path = "/update")
		public BaseResponse updateUser(String userEntity,String modifyReason) throws Exception {
				JSONObject jsonObject = JSONObject.fromObject(userEntity);
				UserEntity user = (UserEntity) JSONObject.toBean(jsonObject, UserEntity.class);
				return new BaseResponse(userHandleService.updateUser(user,modifyReason));
		}
		
		@BaseLog(title = "删除用户",module = ModuleConstants.APP_ADMIN,businessType = BusinessType.DELETE)
		@ApiOperation(value = "管理员删除用户", httpMethod = "DELETE")
		@ApiImplicitParam(name = "userId", value = "用户ID"
						, required = true, dataType = "int")
		@DeleteMapping(path = "/delete/{userId}")
		public BaseResponse deleteUser(@PathVariable int userId){
				return userHandleService.deleteUser(userId);
		}
		
		@ApiOperation(value = "根据条件模糊查询用户", httpMethod = "GET")
		@ApiImplicitParams(value = {@ApiImplicitParam(name = "queryParam", value = "模糊查询参数"
						, required = true, dataType = "String"),@ApiImplicitParam(name = "condition", value = "模糊查询条件"
						, required = true, dataType = "Integer")})
		@GetMapping(path = "/blur/{condition}")
		@SentinelResource(value = "blurQuery",blockHandlerClass = UserLimitingMethods.class,blockHandler = "adminLimitingMethod",
						fallbackClass = UserDowngradeMethods.class,fallback = "adminDowngradeMethod")
		public UserResponse blurQuery(String queryParam,@PathVariable Integer condition){
				return userHandleService.blurSelectUserByUserName(queryParam,condition);
		}
		
		@ApiOperation(value = "分页查询黑名单", httpMethod = "GET")
		@GetMapping(path = "/banPage")
		public R<BanUserPageDto> selectBanUserByPage(Integer currentPage, Integer pageSize){
				return R.ok(userHandleService.findBanUserByPage(currentPage, pageSize));
		}
		
		@ApiOperation(value = "分页查询用户", httpMethod = "GET")
		@GetMapping(path = "/page")
		@ApiImplicitParams(value = {@ApiImplicitParam(name = "currentPage", value = "分页查询当前页数"
						, required = true, dataType = "Integer"),@ApiImplicitParam(name = "pageSize", value = "分页查询每页显示条数"
						, required = true, dataType = "Integer")})
		public R<UserPageDto> selectUserByPage(Integer currentPage, Integer pageSize){
				return R.ok(userHandleService.selectUserByPage(currentPage,pageSize));
		}
		
		@ApiOperation(value = "管理员根据条件精确查询用户", httpMethod = "GET")
		@GetMapping(path = "/exact/{type}")
		@ApiImplicitParam(name = "queryParam", value = "手机号码"
						, required = true, dataType = "String")
		public UserResponse findUserByCondition(String queryParam,@PathVariable UserQueryEnum type){
				return userHandleService.findUserByCondition(queryParam,type);
		}
}
